<?php

/**
 * lender actions.
 *
 * @package    vmdai
 * @subpackage lender
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class lenderActions extends sfActions
{
 /**
  * Executes index action
  *
  * @param sfRequest $request A request object
  */
  public function executeIndex(sfWebRequest $request)
  {
        $dql = Doctrine::getTable('Loan')->createQuery()->orderBy('created_at desc');
        $page = $request->getParameter('page',1);
        $this->pager = new sfDoctrinePager('Loan',11);
        $this->pager->setQuery($dql);
        $this->pager->setPage($page);
        $this->pager->init();
  }
  
  //标审核
  public function executeCheck(sfWebRequest $request){
      $this->forward404Unless($this->loan = Doctrine::getTable('Loan')->findOneById($request->getParameter('id',0)));
      
      if($this->loan->isCheck()){
          $this->redirect('lender/index');
      }
      
      if($request->isMethod('POST')){
          $is_check = (int)$request->getParameter('check_value',0);
          //通过对记录的操作，关联对应的表
          $loan_check_record = new LoanCheckRecord();
          $loan_check_record->setActionUserId(0)->setLoanId($this->loan->getId())->setLoanUserId($this->loan->getUserId())->setStatus($is_check)->save();
          $this->redirect('lender/index');
      }
  }
  
  
  public function executeAdd(sfWebRequest $request){
    $this->loan_purposes = Doctrine::getTable('LoanPurpose')->findAll();
    $this->loan_list_types = Doctrine::getTable('LoanListType')->findAll();
  }
  
  public function executeShow_info(sfWebRequest $request){
    $this->forward404Unless($this->loan = Doctrine::getTable('Loan')->findOneById($request->getParameter('id',0)));
  }
  
  public function executeCreate(sfWebRequest $request){
      if($request->isMethod('POST')){
          
          $user_id = (int)$request->getParameter('user_id',0);
          $purpose = (int)$request->getParameter('purpose',1);
          $list_type = (int)$request->getParameter('list_type',1);
          $title = trim($request->getParameter('title',''));
          $money = (double)$request->getParameter('money',0);
          $is_day = (int)$request->getParameter('is_day',1);
          $term_num = (int)$request->getParameter('term_num',2);
          $money_rate = (double)$request->getParameter('money_rate',0);
          $end_time = (int)$request->getParameter('end_time',0);
          $cash = (int)$request->getParameter('cash',0);
          $service_fee = (double)$request->getParameter('service_fee',0);
          $quota_info = trim($request->getParameter('quota_info'));
          $loan_info = trim($request->getParameter('loan_info'));
          $risk_management_info = trim($request->getParameter('risk_management_info'));
          $user_quota = (double)$request->getParameter('user_quota',0);
          $web_user = $this->getUser();
          
         
          if($user_id ==0){
              $web_user->setFlash('error', '请绑定用户！');
              $this->redirect('lender/add');
          }elseif(strlen($title) < 5){
              $web_user->setFlash('error', '借款标题必须大于5个字符！');
              $this->redirect('lender/add');
          }elseif($money <= 0 || $money > $user_quota){
              $web_user->setFlash('error', '借款金额必须大于0小于用户自身限额！');
              $this->redirect('lender/add');
          }elseif($term_num <= 1){
              $web_user->setFlash('error', '租期必须大于1！');
              $this->redirect('lender/add');
          }elseif($money_rate <= 0){
              $web_user->setFlash('error', '利率必须大于0！');
              $this->redirect('lender/add');
          }elseif($end_time <= 1){
              $web_user->setFlash('error', '投标结束日期必须大于1！');
              $this->redirect('lender/add');
          }
          
          $loan = new Loan();
          $loan->setUserId($user_id)
              ->setPurpose($purpose)
              ->setListType($list_type)
              ->setTitle($title)
              ->setMoney($money)
              ->setMoneyRate($money_rate)
              ->setIsDay($is_day)
              ->setTermNum($term_num)
              ->setEndTime($end_time)
              ->setServiceFee($service_fee)
              ->setCash($cash)
              ->setStatus(1)
              ->setLoanInfo($loan_info)
              ->setQuotaInfo($quota_info)
              ->setRiskManagementInfo($risk_management_info)
              ->setIsNew(true)
              ->save();
          
          if($list_type=="2"){
            //房产抵押
            $estate_name = $request->getParameter('estate_name');
            $build_size = $request->getParameter('build_size');
            $use_year = (int)$request->getParameter('use_year',1);
            $evaluate_price = (double)$request->getParameter('evaluate_price',0);
            $reference_price = (double)$request->getParameter('reference_price',0);
            $house_infos=new HouseInfos();
            $house_infos->setLoanId($loan->getId())
              ->setEstateName($estate_name)
              ->setBuildSize($build_size)
              ->setUseYear($use_year)
              ->setReferencePrice($reference_price)
              ->setEvaluatePrice($evaluate_price)
             ->save();
          }
          $web_user->setFlash('success', '发标成功！');
          $this->redirect('lender/index');
      }
      
      $this->redirect('lender/add');
  }
  
  
   public function executeEdit(sfWebRequest $request){
      $this->loan_id=(int)$request->getParameter('id',0);
    
      $this->forward404Unless($this->loan = Doctrine::getTable('Loan')->findOneById($this->loan_id));
      $this->user_account=Doctrine::getTable('UserAccount')->findOneByUserId($this->loan_id);
      $this->loan_purposes = Doctrine::getTable('LoanPurpose')->findAll();
      $this->loan_list_types = Doctrine::getTable('LoanListType')->findAll();
     
      if($this->loan->getListType()=="2"){
          $this->house_infos=Doctrine::getTable('HouseInfos')->findOneByLoanId($this->loan_id);
      }
       
  }
  
  public function executeSaveloan(sfWebRequest $request){
      if($request->isMethod('POST')){
         $web_user = $this->getUser();
          $loan_id=(int)$request->getParameter('id',0);
          $purpose = (int)$request->getParameter('purpose',1);
          $title = trim($request->getParameter('title',''));
          $cash = (int)$request->getParameter('cash',0);
          $service_fee = (double)$request->getParameter('service_fee',0);
          $quota_info = trim($request->getParameter('quota_info'));
         
          $loan_info = trim($request->getParameter('loan_info'));
          $risk_management_info = trim($request->getParameter('risk_management_info'));
          $loan = Doctrine::getTable('Loan')->findOneById($loan_id);
          $loan->setPurpose($purpose)
               ->setTitle($title)
               ->setServiceFee($service_fee)
               ->setCash($cash)
               ->save();
          $loan_description = Doctrine::getTable('LoanDescription')->findOneByLoanId($loan_id);
          $loan_description->setLoanInfo($loan_info)->setQuotaInfo($quota_info)->setRiskManagementInfo($risk_management_info)->save();
          if($loan->getListType()=="2"){
            //房产抵押
            $estate_name = $request->getParameter('estate_name');
            $build_size = $request->getParameter('build_size');
            $use_year = (int)$request->getParameter('use_year',1);
            $evaluate_price = (double)$request->getParameter('evaluate_price',0);
            $reference_price = (double)$request->getParameter('reference_price',0);
            $house_infos=Doctrine::getTable('HouseInfos')->findOneByLoanId($loan_id);
            $house_infos->setEstateName($estate_name)
              ->setBuildSize($build_size)
              ->setUseYear($use_year)
              ->setReferencePrice($reference_price)
              ->setEvaluatePrice($evaluate_price)
              ->save();
          }
          $web_user->setFlash('success', '发标成功！');
          $this->redirect('lender/index');
      }
       
  }
  
  
}
